package com.ruoyi.web.controller.sms;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.product.domain.StoreProduct;
import com.ruoyi.product.service.IStoreProductService;
import com.ruoyi.sms.domain.SmsTemplate;
import com.ruoyi.sms.service.ISmsTemplateService;
import com.ruoyi.storeclassify.domain.Storeclassify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/sms/smsTemplate")
public class SmsTemplateController extends BaseController {

    @Autowired
    private ISmsTemplateService smsTemplateService;

    @PreAuthorize("@ss.hasPermi('sms:smsTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmsTemplate smsTemplate) {
        startPage();
        List<SmsTemplate> list = smsTemplateService.selectSmsTemplateList(smsTemplate);
//        if(list.size() == 0){
//            return getDataTable(list);
//        }
//        List<Storeclassify> classifies = classifyService.selectSecond();
//        HashMap<Long, String> map = new HashMap<>();
//        classifies.forEach(e->{
//            map.put(e.getId(),e.getClassifyName());
//        });
//        list.forEach(e->{
//            if(map.containsKey(e.getClassifyId())){
//                e.setClassifyName(map.get(e.getClassifyId()));
//            }
//        });
        return getDataTable(list);
    }

    /**
     * 添加
     */
    @PreAuthorize("@ss.hasPermi('sms:smsTemplate:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SmsTemplate smsTemplate){
        return toAjax(smsTemplateService.insertSmsTemplate(smsTemplate));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('sms:smsTemplate:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SmsTemplate smsTemplate)
    {
        if(smsTemplate.getId() == null){
            return toAjax(0);
        }
        return toAjax(smsTemplateService.updateSmsTemplate(smsTemplate));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('sms:smsTemplate:delete')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(smsTemplateService.logicDeleteSmsTemplateByIds(ids));
    }
}
